<?php

namespace frontendm\models;

/**
 * @author  shuang
 * @date    2016-9-6 12:56:56
 * @version V1.0
 * @desc    
 */
use common\models\UserAddress;
use yii\behaviors\TimestampBehavior;
use Yii;

class WB_UserAddress extends UserAddress {
    /*
     * 设置表操作行为动作
     * return array
     */

    public function behaviors() {
        return[
            [
                'class' => TimestampBehavior::className()
            ]
        ];
    }

    //验证表单域
    public function rules() {
        return array_merge(parent::rules(), [
        ]);
    }

    /*
     * 关联字典表   地区
     */

    public function getAreaType() {

        return $this->hasOne(WB_Dictionary::className(), ['id' => 'area']);
    }

    /*
     * 关联用户
     */

    public function getUserID() {

        return $this->hasOne(WB_UserProfile::className(), ['userid' => 'userid']);
    }

    /*
     * 获取用户管理地址
     * @params $userid
     * return array
     */

    public static function getData($userid) {
        return WB_UserAddress::find(["userid" => $userid])->with(["areaType", "userID"])->asArray()->all();
    }

    /*
     * 获取单个用户地址
     */

    public static function getDataOne($id) {
        return WB_UserAddress::find(["id" => $id])->with(["areaType", "userID"])->asArray()->one();
    }

    /*
     * 添加用户管理地址
     */

    public static function createAddress() {
        if ($id = Yii::$app->request->post("id")) {
            $model = WB_UserAddress::findOne($id);
        } else {
            $model = new WB_UserAddress();
        }
        $model->attributes = Yii::$app->request->post();
        $model->userid = 1; //Yii::$app->user->id;
        $model->area = 1;
        if ($model->validate()) {
            return $model->save();
        }
        return false;
    }

}
